<?php

define('PDF_LABEL', 0);
define('PDF_INFO', 1);
define('PDF_EM', 2);
define('PDF_TH', 3);
define('PDF_H', 4);

class MonFPDF extends FPDF{

var $widths;
var $aligns;

var $titre;

function __construct($titre){
	$this->titre = $titre;
	parent::__Construct();
	$this->SetDisplayMode('fullpage');
	$this->SetTitle($this->titre);
	$this->AliasNbPages();
}

function Separateur($hauteur, $titre = '', $avant=0){
	// Ligne séparatrice
	$this->Ln($avant);
	$this->SetStyle(PDF_H);
	$this->Cell(0, $hauteur, $titre, 'T', 1, 'C');
}

function Hr(){
	$this->Ln();
	$this->Line(10, $this->GetY(), 200, $this->GetY());
}

function AddX($i){
	$this->SetX($this->GetX()+$i);
}

function AddY($i){
	$this->SetY($this->GetY()+$i);
}

function SetXrel($rel){
	$this->SetX(10+190*$rel/100);
}

function SetStyle($style){
	switch ($style)
	{
		case PDF_LABEL:
			$this->SetFont('Arial','U',10);
			break;
		case PDF_INFO:
			$this->SetFont('Arial','',10);
			break;
		case PDF_EM:
			$this->SetFont('Arial','I',8);
			break;
		case PDF_TH:
			$this->SetFont('Arial','B',8);
			break;
		case PDF_H:
			$this->SetFont('Arial','B',11);
			break;
	}
}

/*********************
* Gestion du tableau *
*********************/

function SetWidths($w)
{
    //Set the array of column widths
    $this->widths=$w;
}

function SetAligns($a)
{
    //Set the array of column alignments
    $this->aligns=$a;
}

function Row($data)
{
    //Calculate the height of the row
    $nb=0;
    for($i=0;$i<count($data);$i++)
        $nb=max($nb, $this->NbLines($this->widths[$i], $data[$i]));
    $h=5*$nb;
    //Issue a page break first if needed
    $this->CheckPageBreak($h);
    //Draw the cells of the row
    for($i=0;$i<count($data);$i++)
    {
        $w=$this->widths[$i];
        $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
        //Save the current position
        $x=$this->GetX();
        $y=$this->GetY();
        //Draw the border
        $this->Rect($x, $y, $w, $h);
        //Print the text
        $this->MultiCell($w, 5, utf8_decode($data[$i]), 0, $a);
        //Put the position to the right of the cell
        $this->SetXY($x+$w, $y);
    }
    //Go to the next line
    $this->Ln($h);
}

function CheckPageBreak($h)
{
    //If the height h would cause an overflow, add a new page immediately
    if($this->GetY()+$h>$this->PageBreakTrigger)
        $this->AddPage($this->CurOrientation);
}

function NbLines($w, $txt)
{
    //Computes the number of lines a MultiCell of width w will take
    $cw=&$this->CurrentFont['cw'];
    if($w==0)
        $w=$this->w-$this->rMargin-$this->x;
    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
    $s=str_replace("\r", '', $txt);
    $nb=strlen($s);
    if($nb>0 and $s[$nb-1]=="\n")
        $nb--;
    $sep=-1;
    $i=0;
    $j=0;
    $l=0;
    $nl=1;
    while($i<$nb)
    {
        $c=$s[$i];
        if($c=="\n")
        {
            $i++;
            $sep=-1;
            $j=$i;
            $l=0;
            $nl++;
            continue;
        }
        if($c==' ')
            $sep=$i;
        $l+=$cw[$c];
        if($l>$wmax)
        {
            if($sep==-1)
            {
                if($i==$j)
                    $i++;
            }
            else
                $i=$sep+1;
            $sep=-1;
            $j=$i;
            $l=0;
            $nl++;
        }
        else
            $i++;
    }
    return $nl;
}

function Tableau($header,$data,$largeurs=-1,$hauteur=6){
    //Largeurs des colonnes
    if ($largeurs==-1)
		$w = array_fill(0, count($header), 190/count($header));
	else
		$w = $largeurs;
	$this->SetWidths($w);
    //En-tête
    $this->SetStyle(PDF_TH);
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],$hauteur,utf8_decode($header[$i]),1,0,'C');
    $this->Ln();
    //Données
    $this->SetStyle(PDF_INFO);
    foreach($data as $row){
		$this->Row($row);
    }
    //Trait de terminaison
    $this->Cell(array_sum($w),0,'','T');
}

/*********************
* Gestion du tableau *
*********************/

var $B;
var $I;
var $U;
var $UL;
var $OL;
var $li;


//Parseur HTML
function WriteHTML($html, $hauteur=5, $espace=15){
	//Initialisation
    $this->B=0;
    $this->I=0;
    $this->U=0;
    $this->UL=FALSE;
    $this->OL=FALSE;
    $this->li = 0;
	$this->SetFont('Arial');
	// Nettoyage du code html
    $html=str_replace('&nbsp;',' ',$html);
    // Si le texte est brut, pas html
    if ( strpos($html,'<p>')==0 )
		$html = '<p>'.str_replace("\n",'</p><p>',$html).'</p>';
    $html=str_replace("\n",'',$html);
    // On casse le code html en un tableau de balise(pair) et de texte(impair)
    $tab=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
    // On parcourt le tableau obtenu
    foreach($tab as $index=>$element){
        //Texte
        if($index%2==0){
			// Element non vide
            if (count(trim($element))>0)
				// Ecriture de l'élément
				$this->Write($hauteur,html_entity_decode(utf8_decode(ltrim($element))));
        }
        //Balise
        else{
			//fermante
            if($element[0]=='/')
                $this->CloseTag(strtoupper(substr($element,1)),$espace);
            //ouvrante
            else{
				// On separe la balise et ses arguments
				
				$temp = explode(' ',$element, 2);
				if ( count($temp) != 2 )
					$temp[] = '';
                list($tag, $attr)=$temp;
                $this->OpenTag(strtoupper($tag),strtoupper($attr),$hauteur);
            }
        }
    }
}

//Balise ouvrante
function OpenTag($tag,$attr,$hauteur){
	if ( $tag== 'STRONG' )
        $tag = 'B';
	if ( $tag=='EM' )
        $tag = 'I';
	if ( $tag=='SPAN' )
		if ( strpos( $attr, "UNDERLINE" ) )
			$tag = 'U';
	if ( $tag=='UL' || $tag=='OL')
		$this->$tag = TRUE;
	if ($tag=='LI') {
		if($this->li>0){
			$this->write("\n");
			$this->Ln();
		}
		$this->SetFont('Arial');
		$this->li += 1;
		if ($this->OL) {
			$this->Write($hauteur,str_pad($this->li,4,' ', STR_PAD_LEFT).") ");
		}
		elseif ($this->UL)
			$this->Write($hauteur,"   o  ");
		$this->SetFont('Times');
	}
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyleTag($tag,True);
}

//Balise fermante
function CloseTag($tag,$espace){
	if($tag=='STRONG')
        $tag='B';
	if($tag=='EM')
        $tag='I';
	if($tag=='SPAN')
		if ($this->U>0)
			$tag='U';
	if ($tag=='UL' || $tag=='OL')
		$this->$tag = FALSE;
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyleTag($tag,False);
    if($tag=='P' || $tag=='UL' || $tag=='OL'){
		//$this->write("\n");
        $this->Ln($espace);
        $this->li = 0;
	}
}

function SetStyleTag($tag,$enable){
//Modifie le style et sélectionne la police correspondante
    $this->$tag+=($enable ? 1 : -1);
    $style='';
    foreach(array('B','I','U') as $s){
        if($this->$s>0)
            $style.=$s;
    }
    $this->SetFont('',$style);
}

}//class MonFPDF extends FPDF




class MonFPDFHeader extends MonFPDF{
//En-tête
function Header(){
    //Police Arial gras 15
    $this->SetFont('Arial','B',15);
    //Titre
    $this->MultiCell(0,8,utf8_decode($this->titre),1,'C'); 
    //Saut de ligne
    $this->Ln(1);
}

//Pied de page
function Footer(){
    //Positionnement à 1,5 cm du bas
    $this->SetY(-10);
    //Police Arial italique 8
    $this->SetFont('Arial','I',8);
    //Numéro de page
    $this->Cell(0,10,utf8_decode('Page '.$this->PageNo().'/{nb} édité le '.date('d/m/Y')),0,0,'C');
}
}

?>
